package com.youdianstar.app.common.font;

import android.content.Context;
import android.graphics.Typeface;

import java.lang.reflect.Field;

/**
 * File: FontsOverride
 * Author: 82149 Create: 2021/12/7 17:07
 * Changes (from 2021/12/7)
 * --------------------------------------------------
 * description: 字体全局配置发方案 弃用
 * ---------------------------------------------------
 */
public class FontsOverride {
    //FontsOverride.setDefaultFont(this, "DEFAULT", "MyFontAsset.ttf");
    /*
    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:typeface">monospace</item>
    </style>
    */
    public static void setDefaultFont(Context context,
                                      String staticTypefaceFieldName, String fontAssetName) {
        final Typeface regular = Typeface.createFromAsset(context.getAssets(),
                fontAssetName);
        replaceFont(staticTypefaceFieldName, regular);
    }

    protected static void replaceFont(String staticTypefaceFieldName,
                                      final Typeface newTypeface) {
        try {
            final Field staticField = Typeface.class
                    .getDeclaredField(staticTypefaceFieldName);
            staticField.setAccessible(true);
            staticField.set(null, newTypeface);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
